package com.lvyq.javaprint.view.toast;

import javafx.animation.FadeTransition;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

/**
 * @author lvyq 吐司弹框
 * @version 1.0
 * Create by 2024-05-13 10:07
 */
public class ToastView {
    public static  void msg( String content,Stage parentStage){

        // 创建一个标签用于显示吐司消息
        Label toastLabel = new Label();
        toastLabel.setStyle("-fx-background-color: #333333; -fx-text-fill: white; -fx-padding: 10px;");

        // 创建一个新的Stage用于显示吐司消息
        Stage toastStage = new Stage();
        Scene scene1 = new Scene(toastLabel);
        scene1.setFill(Color.TRANSPARENT);
        toastStage.setScene(scene1);
        toastStage.setAlwaysOnTop(true);
        // 确保吐司窗口始终在最顶层
        toastStage.initStyle(StageStyle.TRANSPARENT);
        toastStage.initOwner(parentStage);
        toastStage.setX(parentStage.getX());
        toastStage.setY(parentStage.getY());
        // 设置吐司窗口透明度渐变动画
        FadeTransition fadeIn = new FadeTransition(Duration.seconds(1), toastLabel);
        fadeIn.setFromValue(0.0);
        fadeIn.setToValue(1.0);

        FadeTransition fadeOut = new FadeTransition(Duration.seconds(1), toastLabel);
        fadeOut.setFromValue(1.0);
        fadeOut.setToValue(0.0);
        toastLabel.setText(content);
        toastStage.show();

        fadeIn.play();
        fadeOut.setDelay(Duration.seconds(1.5));
        fadeOut.setOnFinished(e -> {
            toastStage.hide();
            toastStage.close();
        });

        fadeOut.play();

    }
}
